/** 
 * File-Name:APIRepository.java
 *
 * Created on 2011-4-21 上午10:31:04
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package com.exceptionhandler.service.api;

import java.sql.Connection;
import java.util.List;

import org.apache.commons.dbutils.DbUtils;
import org.apache.log4j.Logger;

import com.exceptionhandler.dao.APIDao;
import com.exceptionhandler.dao.support.DaoSupport;
import com.exceptionhandler.entity.APIInfo;

/**
 * Description:
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2011-4-21 上午10:31:04
 */
public class APIRepository {
	private static Logger log = Logger.getLogger(APIRepository.class);
	public static final String SAVE_SUCCESS = "SAVE_SUCCESS";
	public static final String SAVE_ALREADY_EXIST = "SAVE_ALREADY_EXIST";
	public static final String SAVE_INVALID_PARAMETER = "SAVE_INVALID_PARAMETER";

	public static synchronized String save(APIInfo apiInfo) {
		if (apiInfo == null || apiInfo.getMethodName() == null
				|| "".equals(apiInfo.getMethodName())) {
			log.error("The apiInfo parameter is invalid.");
			return SAVE_INVALID_PARAMETER;
		} else {
			String SAVE_SQL = "INSERT INTO apiinfo (uuid, methodName, inputParameters, output, comment, throwExceptions, signature, javaDocUrl, entityStatus, entityDatetime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
			boolean isExist = isExist(apiInfo.getMethodName());
			if (isExist) {
				log.info("API Info Exist! Do NOT Save!");
				return SAVE_ALREADY_EXIST;
			} else {
				Object[] params = { apiInfo.getUuid(),
						apiInfo.getMethodName().trim(),
						apiInfo.getInputParameters(), apiInfo.getOutput(),
						apiInfo.getComment(), apiInfo.getThrowExceptions(),
						apiInfo.getSignature(), apiInfo.getJavaDocUrl(),
						apiInfo.getEntityStatus(), apiInfo.getEntityDatetime() };

				APIDao apiDao = new APIDao();
				apiDao.execute(SAVE_SQL, params);
				return SAVE_SUCCESS;
			}
		}
	}

	public static synchronized boolean isExist(String methodName) {
		String sql = "SELECT * FROM apiinfo WHERE methodName=?";

		Object[] params = { methodName };

		APIDao apiDao = new APIDao();
		List<APIInfo> apiInfos = apiDao.find(sql, params);
		if (apiInfos == null || apiInfos.size() == 0) {
			return false;
		} else if (apiInfos.size() == 1) {
			return true;
		} else {
			log
					.error("The number of results is greater than 1! The expected number is 1!");
			return true;
		}
	}

	public static synchronized APIInfo get(String methodName) {
		APIInfo apiInfo = null;
		APIDao apiDao = new APIDao();
		Connection connection = null;
		try {
			connection = DaoSupport.getConnection();
			List<APIInfo> apiInfos = apiDao.getByProperty(connection,
					"methodName", "'" + methodName + "'");
			if (apiInfos != null && apiInfos.size() == 1) {
				apiInfo = apiInfos.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				DbUtils.close(connection);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}

		return apiInfo;
	}

	/**
	 * Description:
	 * 
	 * @param args
	 *            void
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
